# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=folly
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2025.05.19.00
pkgrel=2
pkgdesc="An open-source C++ library developed and used at Facebook (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://github.com/facebook/folly'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-glog"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         #"${MINGW_PACKAGE_PREFIX}-libbacktrace" # Libraries seems not provide needed functions for symbolizer.
         #"${MINGW_PACKAGE_PREFIX}-libdwarf"
         #"${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-libsodium"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fast_float")
source=("https://github.com/facebook/folly/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        folly.patch)
sha256sums=('c60daf2e709e0a3fd428d09a1e3439c1836189d4248189a1a45f33500de90eac'
            '0afa584acdb7405c571f9e3adbff9f476d9d91eb0100cae35fe4d0e51f6af43f')

prepare() {
    cd ${_realname}-${pkgver}
    patch -p1 -i ${srcdir}/folly.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DPYTHON_EXTENSIONS=OFF \
      -DBUILD_WITH_JEMALLOC=OFF \
      -DUSE_STATIC_DEPS_ON_UNIX=ON \
      -DCMAKE_POLICY_DEFAULT_CMP0167=NEW \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE* -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  sed -s "s|$(cygpath -m ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/lib${_realname}.pc"
}
